use handle_variable::handler::to_snake_case;

pub fn header_to_variable(headers: &[String]) -> Vec<String> {
    let mut new_headers = Vec::new();
    for header in headers {
        new_headers.push(to_snake_case(header));
    }
    new_headers
}

pub fn header_to_model(headers: &[String]) -> Vec<String> {
    let mut models = Vec::new();
    models.push("id = models.AutoField(primary_key=True, unique=True, null=False),".to_string());
    for header in headers {
        let field_str = format!(
            "{} = models.CharField(max_length=50, default=\"\", null=False, help_text=\"\"),",
            to_snake_case(header)
        );
        models.push(field_str);
    }
    models
}

pub fn header_to_sql(headers: &[String], table_name: &str) -> Vec<String> {
    let mut sqls = Vec::new();
    sqls.push(format!("create table if not exists {}(", table_name));
    sqls.push("id INT AUTO_INCREMENT PRIMARY KEY,".to_string());

    // let sql_bodys:Vec<String> = headers.iter().map(|header| {
    //     format!("{} VARCHAR(255) DEFAULT NULL COMMENT '',", to_snake_case(header))
    // }).collect();
    
    
    let mut headers_iter = headers.into_iter().peekable();

    while let Some (header) = headers_iter.next() {

        let fields = match headers_iter.peek() {
            Some(_) => {
                format!(
                    "{} VARCHAR(255) DEFAULT NULL COMMENT '',",
                    to_snake_case(header)
                )
            },
            None => {
                format!(
                    "{} VARCHAR(255) DEFAULT NULL COMMENT ''",
                    to_snake_case(header)
                )
            }
        };
        sqls.push(fields);
        //
        //
        //
        // if headers_iter.peek().is_some() {
        //     sqls.push(format!(
        //         "{} VARCHAR(255) DEFAULT NULL COMMENT '',",
        //         to_snake_case(header)
        //     ));
        // }else {
        //     sqls.push(format!(
        //         "{} VARCHAR(255) DEFAULT NULL COMMENT ''",
        //         to_snake_case(header)
        //     ));
        // }
    }
    sqls.push(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='';".to_string());
    sqls
}
